package com.wfcm.wxUitls;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;

import com.wfcm.utils.ConstantUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSONObject;
import com.wfcm.entity.WfWxShareEntity;
import com.wfcm.service.WfWxShareService;
import com.wfcm.utils.FastJSONUtils;
import com.wfcm.utils.WxUtil;

public class TaskJob {

	private static Logger log = LoggerFactory.getLogger(TokenThread.class);  
	
	@Autowired
	WfWxShareService wxShareService;
	
    // 第三方用户唯一凭证  
	//appid/appsecret为微信app ; 配置获取access_token所需参数appid和appsecret
	public static String appid = ConstantUtil.GG_APP_ID;//开发者appId正式
    // 第三方用户唯一凭证密钥  
	public static String appsecret = ConstantUtil.GG_APP_SECRET;//开发者app_SECRET正式
	
	public void getAccessToken() {
		WfWxShareEntity accessToken = new WfWxShareEntity();  
		accessToken = WxUtil.getAccessToken(appid, appsecret); 
	    if (null != accessToken ) {  
	        log.info("获取access_token成功，有效时长{}秒 token:{}", accessToken.getContent());
			//System.out.println("************:"+accessToken.getId()+"***:"+accessToken.getName());
	        wxShareService.update(accessToken);
	    }
		System.out.println("******************任务进行中****************");
	}
	
	public void getJsapiTicket() {
		WfWxShareEntity accessToken = new WfWxShareEntity();  
		accessToken = wxShareService.queryObject(1);
		
		JSONObject jsonObj= FastJSONUtils.getJSONObject(accessToken.getContent());  
        String access_token = jsonObj.getString("access_token");
        
        WfWxShareEntity jsapiTicket = new WfWxShareEntity();
		jsapiTicket = WxUtil.getTicket(access_token);

	    if (null != jsapiTicket ) {  
	        log.info("获取jsapiTicket成功，有效时长{}秒 token:{}", jsapiTicket.getContent());
	    }
		System.out.println("******************任务进行中****************");
	    wxShareService.update(jsapiTicket);
	}
	
	public void job() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		Date now = new Date();
		System.out.println("******************任务进行中****************"+sdf.format(now));
	}
	
	public void job2() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		Date now = new Date();
		System.out.println("******************任务进行中****************"+sdf.format(now));
	}

	/*public static void main(String[] args) {

		new TaskJob().getAccessToken();
	}*/

	public void getIp(){
        Enumeration<NetworkInterface> netInterfaces = null;
        try {
            netInterfaces = NetworkInterface.getNetworkInterfaces();
            while (netInterfaces.hasMoreElements()) {
                NetworkInterface ni = netInterfaces.nextElement();
                System.out.println("DisplayName:" + ni.getDisplayName());
                System.out.println("Name:" + ni.getName());
                Enumeration<InetAddress> ips = ni.getInetAddresses();
                while (ips.hasMoreElements()) {
                    System.out.println("IP:"
                            + ips.nextElement().getHostAddress());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
          new  TaskJob().getIp();
    }
}
